import socket
import json
import datetime
from dataclasses import dataclass


def send_data_to_java(data):
    # Java服务器的IP地址和端口号
    JAVA_SERVER_IP = 'localhost'
    JAVA_SERVER_PORT = 8000  # 请替换为实际的Java服务器端口

    # 创建socket对象
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        # 连接到Java服务器
        sock.connect((JAVA_SERVER_IP, JAVA_SERVER_PORT))

        # 将数据转换为字节串（假设数据是字符串）
        data_bytes = data.encode('utf-8')

        # 发送数据到Java服务器
        sock.sendall(data_bytes)

        # （可选）接收Java服务器的回复（如果有的话）
        # response = sock.recv(1024).decode('utf-8')
        # print(f"Received response: {response}")

    finally:
        # 关闭socket连接
        sock.close()


# 示例数据
data_to_send = "{'stock_symbol': 'AAPL', 'price': 145.23, 'volume': 100}"

# 打印tick数据
# print('tick :', tick, sep='\n')
#
tick ={
    'quotes': [{
        'bid_p': 7.36,
        'bid_v': 24700,
        'ask_p': 7.37,
        'ask_v': 260200,
        'bid_q': {
            'total_orders': 0,
            'queue_volumes': []
        },
        'ask_q': {
            'total_orders': 0,
            'queue_volumes': []
        }
    }, {
        'bid_p': 7.35,
        'bid_v': 324000,
        'ask_p': 7.38,
        'ask_v': 129200,
        'bid_q': {
            'total_orders': 0,
            'queue_volumes': []
        },
        'ask_q': {
            'total_orders': 0,
            'queue_volumes': []
        }
    }, {
        'bid_p': 7.34,
        'bid_v': 323200,
        'ask_p': 7.39,
        'ask_v': 106200,
        'bid_q': {
            'total_orders': 0,
            'queue_volumes': []
        },
        'ask_q': {
            'total_orders': 0,
            'queue_volumes': []
        }
    }, {
        'bid_p': 7.33,
        'bid_v': 176500,
        'ask_p': 7.4,
        'ask_v': 164980,
        'bid_q': {
            'total_orders': 0,
            'queue_volumes': []
        },
        'ask_q': {
            'total_orders': 0,
            'queue_volumes': []
        }
    }, {
        'bid_p ': 7.32,
        'bid_v ': 354200,
        'ask_p ': 7.41,
        'ask_v ': 108900,
        'bid_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        },
        'ask_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        }
    }, {
        'bid_p ': 0,
        'bid_v': 0,
        'ask_p': 0,
        'ask_v': 0,
        'bid_q': {
            'total_orders': 0,
            'queue_volumes': []
        },
        'ask_q': {
            'total_orders': 0,
            'queue_volumes': []
        }
    }, {
        'bid_p': 0,
        'bid_v': 0,
        'ask_p ': 0,
        'ask_v ': 0,
        'bid_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        },
        'ask_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        }
    }, {
        'bid_p ': 0,
        'bid_ask_p ': 0,
        'ask_v ': 0,
        'bid_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        },
        'ask_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        }
    }, {
        'bid_p ': 0,
        'bid_v ': 0,
        'ask_p ': 0,
        'ask_v ': 0,
        'bid_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        },
        'ask_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        }
    }, {
        'bid_p ': 0,
        '_v ': 0,
        'ask_p ': 0,
        'ask_v ': 0,
        'bid_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        },
        'ask_q ': {
            'total_orders ': 0,
            'queue_volumes ': []
        }
    }],
    'symbol ': 'SHSE .600000 ',
    'created_at': datetime.datetime(2024, 4, 18, 14, 3, 14, 910000, tzinfo = datetime.timezone(datetime.timedelta(seconds = 28800), 'Asia / Shanghai ')),
    'price ': 7.37,
    'open ': 7.34,
    'high ': 7.46,
    'low ': 7.31,
    'cum_volume ': 40922245,
    'cum_amount ': 302107704.0,
    'cum_position ': 0,
    'lastamount ': 2948.0,
    'last_volume ': 400,
    'trade_type ': 7,
    'flag ': 0,
    'receive_local_time ': 1713420194.839099,
    'iopv ': 0.0
}

# setattr(tick,"symbol",None)
# delattr(tick, 'created_at')
# del tick.symbol
# print('tick_del :', tick, sep='\n')

del tick["created_at"]
print(tick)

# 使用json.loads直接转换，JSON模块会自动处理单引号的转义
# json_data = json.dumps(tick)
# print('json_data :', json_data)
#
#
#
send_data_to_java(data_to_send)
